/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MGxRPPacket.h"

//---------------------------------------------------------------------------

MGxRPPacket::MGxRPPacket(void)
{
	Initialize();
}

MGxRPPacket::~MGxRPPacket(void)
{
	Initialize();
}

//---------------------------------------------------------------------------

void MGxRPPacket::Initialize(void)
{
	ProtocolType = __gxrpNull;
	ProtocolID = 0;
	AttributeType = 0;
	Attributes.Clear();
}

bool MGxRPPacket::IsEmpty(void)
{
	return ProtocolType==__gxrpNull ? true : false;
}

//---------------------------------------------------------------------------

bool MGxRPPacket::AddGvrpAttribute(BYTE lenIn, BYTE eventIn, BYTE val0In, BYTE val1In)
{
	MLEVRecord record;

	record.xLength   = lenIn;
	record.xEvent    = eventIn;
	record.xValue[0] = val0In;
	record.xValue[1] = val1In;

	return Attributes.Add(&record);
}

bool MGxRPPacket::AddGmrpAttribute(BYTE lenIn, BYTE eventIn, BYTE valIn)
{
	MLEVRecord record;

	record.xLength   = lenIn;
	record.xEvent    = eventIn;
	record.xValue[0] = valIn;
	record.xValue[1] = 0;

	return Attributes.Add(&record);
}

//---------------------------------------------------------------------------

bool MGxRPPacket::LoadFromPointer(MGxRPPacket* packIn)
{
	Initialize();
	if(packIn==NULL) return true;

	MLEVRecord *rec;
	int i, cnt;
	bool err;

	ProtocolType  = packIn->ProtocolType;
	ProtocolID    = packIn->ProtocolID;
	AttributeType = packIn->AttributeType;

	Attributes.Clear();

	err = false;
	cnt = packIn->Attributes.GetSize();
	for(i=0; i<cnt && !err; i++){
		rec = packIn->Attributes.GetByIdx(i);
		if(rec==NULL) err = true;
		else{
			if(!Attributes.Add(rec))
				err = true;
		}
	}

	return !err;
}

//---------------------------------------------------------------------------

bool MGxRPPacket::IsEqualWith(MGxRPPacket* packIn)
{
	int idx, cnt1, cnt2;
	bool done, theSame;

	if(packIn==NULL) return false;

	if(ProtocolType !=packIn->ProtocolType)  return false;
	if(ProtocolID   !=packIn->ProtocolID)    return false;
	if(AttributeType!=packIn->AttributeType) return false;

	cnt1 = Attributes.GetSize();
	cnt2 = packIn->Attributes.GetSize();

	if(cnt1!=cnt2) return false;

	MLEVRecord *rec1;
	MLEVRecord *rec2;

	done = false;
	theSame = true;
	idx = 0;
	while(!done){
		rec1 = Attributes.GetByIdx(idx);
		rec2 = packIn->Attributes.GetByIdx(idx);
		if(rec1==NULL){
			// suppose theSame because rec1 should NOT be null so it is better to replace it !
			done = true;
		}
		else{
			if(!rec1->IsEqualWith(rec2)){
				done = true;
				theSame = false;
			}
			else{
				idx++;
				if(idx>=cnt1)
					done = true;
			}
		}
	}

	return theSame;
}

//---------------------------------------------------------------------------
